﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DeepZoomPivotConstructor;
using System.Drawing;
using DeepZoomPivotConstructor.VisualLib.Hotspots;
using System.IO;
using DeepZoomPivotConstructor.VisualLib;

namespace t_VisualLib
{
    /// <summary>
    /// Summary description for t_VisualTitleAndBackground
    /// </summary>
    [TestClass]
    public class t_VisualTitleAndBackground
    {
        public t_VisualTitleAndBackground()
        {
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void TestBadCreation()
        {
            ///
            /// Craete with a null visual - bad!
            /// 

            VisualTitleAndBackground t = new VisualTitleAndBackground(null);
        }

        [TestMethod]
        public void TestGoodCreation()
        {
            VisualBitmapImage bogus = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
            VisualTitleAndBackground t = new VisualTitleAndBackground(bogus);
        }

        [TestMethod]
        public void TestBasicParameters()
        {
            VisualBitmapImage bogus = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
            VisualTitleAndBackground t = new VisualTitleAndBackground(bogus);

            Assert.AreEqual(bogus.Width + t.XBoarder * 2.0, t.Width, "Width not right");
            Assert.AreEqual(bogus.Height + t.YBoarder * 2.0, t.Height, "Width not right");
        }

        [TestMethod]
        public void TestBasicParametersUpdateXY()
        {
            VisualBitmapImage bogus = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
            VisualTitleAndBackground t = new VisualTitleAndBackground(bogus);

            t.XBoarder = 0.25;
            t.YBoarder = 0.4;

            Assert.AreEqual(bogus.Width + t.XBoarder * 2.0, t.Width, "Width not right");
            Assert.AreEqual(bogus.Height + t.YBoarder * 2.0, t.Height, "Width not right");
        }

        [TestMethod]
        public void TestBasicZ()
        {
            VisualBitmapImage bogus = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
            VisualTitleAndBackground t = new VisualTitleAndBackground(bogus);

            Assert.AreEqual(2, t.SGNodeList.Count, "Expected 2 images");
            int zindex1 = t.SGNodeList[0].ZOrder;
            int zindex2 = t.SGNodeList[1].ZOrder;
            Assert.AreNotEqual(zindex1, zindex2, "The two z indicies should not be equal!");

            if (t.SGNodeList[0].Width == 1.0)
            {
                Assert.IsTrue(zindex1 > zindex2, "Expected the bogus image to have a higher z index!");
            }
            else
            {
                Assert.IsTrue(zindex1 < zindex2, "Expected the bogus image to have a higher z index!");
            }
        }

        [TestMethod]
        public void TestEnumeration()
        {
            VisualBitmapImage bm = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
            VisualTitleAndBackground t = new VisualTitleAndBackground(bm);

            var allVis = from v in t.GetHierarchicalEnumerator(ignore => false)
                         select v;

            Assert.IsTrue(allVis.Count() >= 1, "Needed to have at least one sub image!");
            Assert.IsNotNull(allVis.FirstOrDefault(v => v.GetType() == typeof(VisualBitmapImage)), "Could not find a bitmap image in there!");
        }

        [TestMethod]
        public void TestFunnyZ()
        {
            VisualBitmapImage bogus = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
            bogus.IncZIndex(10);
            VisualTitleAndBackground t = new VisualTitleAndBackground(bogus);

            int zindex1 = t.SGNodeList[0].ZOrder;
            int zindex2 = t.SGNodeList[1].ZOrder;
            Assert.AreEqual(1, Math.Abs(zindex1 - zindex2), "Expect only a difference of 1 between the background and subect image");
        }

        [TestMethod]
        public void TestCreateWithSimpleText()
        {
            VisualBitmapImage bogus = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
            VisualTitleAndBackground t = new VisualTitleAndBackground(bogus)
            {
                {0.5, "hi there"}
            };

            double finalSize = t.YBoarder * 3.0 + bogus.Height + 0.5;
            double delta = Math.Abs(finalSize - t.Height);
            Assert.IsTrue(delta < 0.1, "Height didn't account for the text!");
        }

        [TestMethod]
        public void TestCreateWithTwoLinesText()
        {
            VisualBitmapImage bogus = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
            VisualTitleAndBackground t = new VisualTitleAndBackground(bogus)
            {
                {0.5, "hi there"},
                {0.25, "Dude"}
            };

            double finalSize = t.YBoarder * 3.0 + bogus.Height + 0.5 + 0.25;
            double delta = Math.Abs(finalSize - t.Height);
            Assert.IsTrue(delta < 0.1, "Height didn't account for the text!");
        }

        [TestMethod]
        public void TestCreateTitleWithHS()
        {
            VisualBitmapImage bogus = new VisualBitmapImage(10.0, 5.0, 100, Color.Black);
            VisualTitleAndBackground t = new VisualTitleAndBackground(bogus)
            {
                {0.5, "hi there", new HSActionLink(new Uri("http://www.nytimes.com"))}
            };

            var hs = from h in t.GetHierarchicalEnumerator(ignore => false)
                     where h.GetType() == typeof(VisualHotSpot)
                     select h as VisualHotSpot;

            Assert.AreEqual(1, hs.Count(), "Expecting a single hot spot");
            var vhs = hs.First();
            Assert.AreEqual(10.0, vhs.Width, "Width of HS isn't right");
            Assert.AreEqual(0.5, vhs.Height, "Height of HS isn't right either!");

            int count = 0;
            vhs.GetAllActoinData(ignore => { count = count + 1; });
            Assert.AreEqual(1, count, "Expected only one hs action!");
        }

        [TestMethod]
        public void TestInfo()
        {
            VisualBitmapImage bogus = new VisualBitmapImage(10.0, 5.0, 100, Color.Black);
            bogus.Info = new Visual.VisualInfo()
            {
                Title = "hi"
            };
            VisualTitleAndBackground t = new VisualTitleAndBackground(bogus);

            Assert.AreEqual("hi", t.Info.Title, "The title wasn't copied over");
        }

        [TestMethod]
        public void TestTempDir()
        {
            VisualBitmapImage bogus = new VisualBitmapImage(10.0, 5.0, 100, Color.Black);
            DirectoryInfo dir = new DirectoryInfo(".\\TestTempDir");
            dir.Create();
            bogus.ImageWritingDirectory = dir;
            VisualTitleAndBackground t = new VisualTitleAndBackground(bogus);
            t.ImageWritingDirectory = dir;
            Assert.AreEqual(2, t.SGNodeList.Count, "expected a single image!");
            Assert.AreEqual(2, dir.EnumerateFiles().Count(), "Expected two jpg file in the proper location");
        }
    }
}
